home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz Kr0nlcKLeZ 1 / HaCKeRz Kr0nlcKLeZ.iso / chibacity / gbbdisk.arj / INTRBOOT / BASIC.ASM next >
Encoding:
Assembly Source File  |  1995-04-12  |  9.1 KB  |  181 lines

  1. ;A Basic Boot Sector for DOS 2.0 to 6.22. This is non-viral!
  2. ;
  3. ;(C) 1995 American Eagle Publications, Inc. All Rights Reserved!
  4.  
  5.  
  6. ;This segment is where the first operating system file (IO.SYS) will be
  7. ;loaded and executed from. We don't know (or care) what is there, as long as
  8. ;it will execute at 0070:0000H, but we do need the address to jump to defined
  9. ;in a separate segment so we can execute a far jump to it.
  10. DOS_LOAD        SEGMENT AT 0070H
  11.                 ASSUME  CS:DOS_LOAD
  12.  
  13.                 ORG     0
  14.  
  15. LOAD:                                   ;Start of the first operating system program
  16.  
  17. DOS_LOAD        ENDS
  18.  
  19.  
  20. MAIN            SEGMENT BYTE
  21.                 ASSUME  CS:MAIN,DS:MAIN,SS:NOTHING
  22.  
  23.  
  24. ;This is the loader for the boot sector. It writes the boot sector to
  25. ;the A: drive in the right place, after it has set up the basic disk
  26. ;parameters. The loader is what gets executed when this program is executed
  27. ;from DOS as a COM file.
  28.  
  29.                 ORG     100H
  30.  
  31. LOADER:
  32.                 mov     ax,201H         ;load the existing boot sector
  33.                 mov     bx,OFFSET DISK_BUF      ;into this buffer
  34.                 mov     cx,1            ;Drive 0, Track 0, Head 0, Sector 1
  35.                 mov     dx,0
  36.                 int     13H
  37.                 mov     ax,201H         ;try twice to compensate for disk
  38.                 int     13H             ;change errors
  39.  
  40.                 mov     si,OFFSET DISK_BUF + 11
  41.                 mov     di,OFFSET BOOTSEC + 11
  42.                 mov     cx,19
  43.                 rep     movsb           ;move disk data to new boot sector
  44.  
  45.                 mov     ax,301H         ;and write new boot sector to disk
  46.                 mov     bx,OFFSET BOOTSEC
  47.                 mov     cx,1
  48.                 mov     dx,0
  49.                 int     13H
  50.  
  51.                 mov     ax,4C00H        ;now exit to DOS
  52.                 int     21H
  53.  
  54.  
  55. ;This area is reserved for loading the boot sector from the disk which is going
  56. ;to be modified by the loader, as well as the first sector of the root directory,
  57. ;when checking for the existence of system files and loading the first system
  58. ;file. The location is fixed because this area is free at the time of the
  59. ;execution of the boot sector.
  60.  
  61.                 ORG     0500H
  62.  
  63. DISK_BUF:       DB      ?                       ;Start of the buffer
  64.  
  65. ;Here is the start of the boot sector code. This is the chunk we will take out
  66. ;of the compiled COM file and put it in the first sector on a floppy disk.
  67.  
  68.                 ORG     7C00H
  69.  
  70. BOOTSEC:        JMP     SHORT BOOT              ;Jump to start of boot sector code
  71.                 NOP                             ;We always want 3 bytes here
  72.  
  73. DOS_ID:         DB      'Am Eagle'              ;Name of this boot sector (8 bytes)
  74. SEC_SIZE:       DW      200H                    ;Size of a sector, in bytes
  75. SECS_PER_CLUST: DB      2                       ;Number of sectors in a cluster
  76. FAT_START:      DW      1                       ;Starting sector for the first File Allocation Table (FAT)
  77. FAT_COUNT:      DB      2                       ;Number of FATs on this disk
  78. ROOT_ENTRIES:   DW      70H                     ;Number of root directory entries
  79. SEC_COUNT:      DW      2D0H                    ;Total number of sectors on this disk
  80. DISK_ID:        DB      0FDH                    ;Disk type code (This is 360KB)
  81. SECS_PER_FAT:   DW      2                       ;Number of sectors per FAT
  82. SECS_PER_TRK:   DW      9                       ;Sectors per track for this drive
  83. HEADS:          DW      2                       ;Number of heads (sides) on this drive
  84. HIDDEN_SECS:    DW      0                       ;Number of hidden sectors on the disk
  85.  
  86. ;Here is the start of the boot sector executable code
  87.  
  88. BOOT:           CLI                                     ;interrupts off
  89.                 XOR     AX,AX                           ;prepare to set up segments
  90.                 MOV     ES,AX                           ;set DS=ES=SS=0
  91.                 MOV     DS,AX
  92.                 MOV     SS,AX                           ;start stack at 0000:7C00
  93.                 MOV     SP,OFFSET BOOTSEC
  94.                 STI                                     ;now turn interrupts on
  95.  
  96. ;Here we look at the first file on the disk to see if it is the first MS-DOS
  97. ;system file, IO.SYS.
  98. LOOK_SYS:
  99.                 MOV     AL,BYTE PTR [FAT_COUNT]         ;get fats per disk
  100.                 XOR     AH,AH
  101.                 MUL     WORD PTR [SECS_PER_FAT]         ;multiply by sectors per fat
  102.                 ADD     AX,WORD PTR [HIDDEN_SECS]       ;add hidden sectors
  103.                 ADD     AX,WORD PTR [FAT_START]         ;add starting fat sector
  104.  
  105.                 PUSH    AX                              ;start of root dir in ax
  106.                 MOV     BP,AX                           ;save it here
  107.  
  108.                 MOV     AX,20H                          ;dir entry size
  109.                 MUL     WORD PTR [ROOT_ENTRIES]         ;dir size in ax
  110.                 MOV     BX,WORD PTR [SEC_SIZE]          ;sector size
  111.                 ADD     AX,BX                           ;add one sector
  112.                 DEC     AX                              ;decrement by 1
  113.                 DIV     BX                              ;ax=# sectors in root dir
  114.                 ADD     BP,AX                           ;now bp is start of data
  115.                 MOV     BX,OFFSET DISK_BUF              ;set up disk read buffer at 0000:0500
  116.                 POP     AX                              ;ax=start of root dir
  117.                 CALL    CONVERT                         ;and go convert sequential sector number to bios data
  118.                 INT     13H                             ;read 1st root sector
  119.                 JC      $
  120.  
  121.                 MOV     DI,BX                           ;compare first file on disk with
  122.                 MOV     CX,11                           ;required file name
  123.                 MOV     SI,OFFSET SYSFILE_1             ;of first system file for PC DOS
  124.                 REPZ    CMPSB
  125.                 JNZ     $                               ;not the same - an error, so hang the machine
  126.  
  127. ;Ok, system file is there, so load it
  128. LOAD_SYSTEM:
  129.                 MOV     AX,WORD PTR [DISK_BUF+1CH]      ;get file size of IO.SYS
  130.                 XOR     DX,DX
  131.                 DIV     WORD PTR [SEC_SIZE]             ;and divide by sector size
  132.                 INC     AX                              ;ax=number of sectors to read
  133.                 CMP     AX,39H                          ;don't load too much!!
  134.                 JLE     LOAD1                           ;not more than 7C00H-700H
  135.                 MOV     AX,39H                          ;plus some room for stack!
  136. LOAD1:          MOV     DI,AX                           ;store that number in BP
  137.                 PUSH    BP                              ;save data start for IO.SYS
  138.                 MOV     BX,700H                         ;set disk read buffer to 0000:0700
  139. RD_IOSYS:       MOV     AX,BP                           ;and get sector to read
  140.                 CALL    CONVERT                         ;convert to bios Trk/Cyl/Sec info
  141.                 INT     13H                             ;and read a sector
  142.                 JC      $                               ;halt on error
  143.                 INC     BP                              ;increment sector to read
  144.                 ADD     BX,WORD PTR [SEC_SIZE]          ;and update buffer address
  145.                 DEC     DI                              ;decrement number of sectors to read
  146.                 JNZ     RD_IOSYS                        ;and go for another if needed
  147.  
  148. ;Ok, IO.SYS has been read in, now transfer control to it
  149. DO_BOOT:
  150.                 MOV     CH,BYTE PTR [DISK_ID]           ;Put drive type in ch
  151.                 MOV     DL,0                            ;Drive number in dl
  152.                 POP     BX                              ;Start of data in bx
  153.                 JMP     FAR PTR LOAD                    ;far jump to IO.SYS
  154.  
  155. ;Convert sequential sector number in ax to BIOS Track, Head, Sector information.
  156. ;Save track number in CH, head in DH, sector number in CH, set AX to 201H. Since
  157. ;this is for floppies only, we don't have to worry about track numbers greater
  158. ;than 255.
  159. CONVERT:
  160.                 XOR     DX,DX
  161.                 DIV     WORD PTR [SECS_PER_TRK]         ;divide ax by sectors per track
  162.                 INC     DL                              ;dl=sector number to start read on, al=track/head count
  163.                 MOV     CL,DL                           ;save sector here
  164.                 XOR     DX,DX
  165.                 DIV     WORD PTR [HEADS]                ;divide ax by head count
  166.                 MOV     DH,DL                           ;head to dh
  167.                 XOR     DL,DL                           ;drive in dl (0)
  168.                 MOV     CH,AL                           ;track to ch
  169.                 MOV     AX,201H                         ;ax="read 1 sector"
  170.                 RET
  171.  
  172. SYSFILE_1       DB      'IO      SYS'                   ;MS DOS System file
  173.  
  174.                 ORG     7DFEH
  175.  
  176. BOOT_ID         DW      0AA55H                          ;Boot sector ID word
  177.  
  178. MAIN            ENDS
  179.  
  180.                 END     LOADER
  181.